<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;

class Product extends Common
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
        $product = db('product')->alias('p')->field('p.name, p.id, p.is_index, pi.image')->join('osl_product_image pi', 'p.id = pi.product_id and pi.is_main = 1', 'LEFT')->paginate(10);
        $this->assign('product', $product);
        return view();
    }

    // 添加/编辑客服
    public function save_product() {

        $id = input('param.id');
        if (Request::instance()->isPost()) {
            $cate_id = input('param.cate_id', 0);
            $cate = db('category')->where('id', $cate_id)->where('level', 2)->find();
            if (!$cate) {
                return json(['code' => 0, 'msg' => '分类不存在或者不是二级分类，请确认！']);
            }
            // 提交数据
            $data = [
                'name' => input('param.name'),
                'cate_id' => $cate_id,
                'add_time' => time(),
                'is_index' => input('param.is_index', 0)
            ];

            if ($id > 0)  {
                $text = '编辑';
                $rs = db('product')->where('id', $id)->update($data);
                $url = url('index');
                $con = db('product_content')->where('product_id', $id)->find();
                if ($con) {
                    db('product_content')->where('product_id', $id)->update(['content'=> input('param.content', '')]);
                } else{
                    db('product_content')->insert(['content'=> input('param.content', ''), 'product_id' => $id]);
                }
            } else {
                $text = '添加';
                $rs = db('product')->insertGetId($data);
                $url = url('save_product', 'id=' . $rs);
                db('product_content')->insert(['content'=> input('param.content', ''), 'product_id' => $rs]);
            }
            if ($rs)
                return json(['code' => 200, 'msg' => $text . '产品成功！', 'url' => $url]);
            else
                return json(['code' => 0, 'msg' => $text . '产品失败！']);
        } else {
            $product = ['cate_id' => 0, 'is_index' => 0, 'id' => 0];
            if ($id > 0) {
                $product = db('product')->alias('p')->field('p.*, pc.content')
                    ->join('osl_product_content pc', 'p.id = pc.product_id', 'LEFT')->where('p.id', $id)->find();
            }
            $this->assign('product', $product);
            // 获取分类树
            $ztree = Ztree($product['cate_id'], false, 1);
            $this->assign('ztree', $ztree);

            return view();
        }
    }

    // 上传产品图片
    public function upload_product_img() {
        $id = input('param.id');
        $files = request()->file('file');
        if ($files) {
            foreach($files as $file) {
                // 移动到框架应用根目录/public/uploads/ 目录下
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'product');
                if ($info) {
                    $data[] = [
                        'image' => $info->getSaveName(),
                        'product_id' => $id
                    ];
                } else {
                    // 上传失败获取错误信息
                    return json(['code' => 0, 'msg' => $file->getError()]);
                }
            }

            $rs = db('product_image')->insertAll($data);
            if ($rs !== false) {
                return json(['code' => 200, 'msg' => '上传成功']);
            } else {
                return json(['code' => 0, 'msg' => '上传失败']);
            }
        }
        return json(['code' => 0, 'msg' => '请选择要上传的文件']);
    }
    
    // 获取图片列表
    public function get_img_list() {
        $id = input('param.id');
        $images = db('product_image')->where('product_id', $id)->order('is_main desc')->select();
        if ($images !== false) {
            return json(['code' => 200, 'data' => $images]);
        } else {
            return json(['code' => 0, 'msg' => '获取失败']);
        }
    }

    // 设置产品是否首页显示
    public function save_product_index() {
        $id = input('param.id');

        $rs = db('product')->where('id', $id)->update(['is_index' => input('param.is_index', 0)]);

        if ($rs !== false) {
            return json(['code' => 200, 'msg' => '修改成功']);
        } else {
            return json(['code' => 0, 'msg' => '修改失败']);
        }
    }

    // 设置产品主图
    public function save_main_img() {
        $id = input('param.id');

        $product_id = db('product_image')->where('id', $id)->value('product_id');

        db('product_image')->where( 'product_id', $product_id)->update(['is_main' => 0]);

        $rs = db('product_image')->where('id', $id)->update(['is_main' => 1]);
        if ($rs !== false) {
            return json(['code' => 200, 'msg' => '修改成功']);
        } else {
            return json(['code' => 0, 'msg' => '修改失败']);
        }
    }

    // 删除图片
    public function del_img() {
        $id = input('param.id');
        $image = db('product_image')->where('id', $id)->find();
        if ($image['is_main'] == 1) {
            return json(['code' => 0, 'msg' => '产品主图不允许']);
        }

        $rs = db('product_image')->where('id', $id)->delete();

        if ($rs !== false) {
            return json(['code' => 200, 'msg' => '删除成功']);
        } else {
            return json(['code' => 0, 'msg' => '删除失败']);
        }
    }
}
